package com.zing.service.impl;

import com.zing.constant.StatusConstant;
import com.zing.entity.Orders;
import com.zing.mapper.DishMapper;
import com.zing.mapper.OrderMapper;
import com.zing.mapper.SetmealMapper;
import com.zing.mapper.UserMapper;
import com.zing.service.WorkspaceService;
import com.zing.vo.BusinessDataVO;
import com.zing.vo.DishOverViewVO;
import com.zing.vo.OrderOverViewVO;
import com.zing.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    // 根据时间段统计营业数据
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询订单总数
        Integer totalOrderCount = orderMapper.countByMap(map);

        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(map);

        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){

            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;  // 订单完成率
            unitPrice = turnover / validOrderCount; // 平均客单价
        }

        //新增用户数查询
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    // 查询订单管理模块数据
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        Integer waitingReceiveOrders = orderMapper.countByMap(map); // 待接单

        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);  //待派送

        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);     //已完成

        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);  // 已取消

        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map); // //全部订单

        return OrderOverViewVO.builder()
                .waitingOrders(waitingReceiveOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    // 查询菜品总览
    public DishOverViewVO getOverviewDishes() {
        Integer enableNum = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer disableNum = dishMapper.countByStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(enableNum)
                .discontinued(disableNum)
                .build();
    }

    // 查询套餐总览
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer enableNum = setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer disableNum = setmealMapper.countByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(enableNum)
                .discontinued(disableNum)
                .build();
    }
}
